package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.ChessArray;
import model.User;
import util.SocketUtil;
import util.StateCode;

/**
 * 
 * @author Administrator
 *
 */
public class ChessPanel extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private GridPanel gridPanel;
	private JLabel[] player = new JLabel[2];
	private String[] s1 = { "开始", "悔棋", "和棋", "认输", "回放" };
	private JButton button[] = new JButton[s1.length];
	private ChessArray chessarray;
	private JLabel[] chessCase = new JLabel[2];
	private ImageIcon black;
	private ImageIcon white;

	public ChessPanel() {
		black = new ImageIcon(getClass().getResource("../image/blackChess.png"));
		white = new ImageIcon(getClass().getResource("../image/whiteChess.png"));
		setBackground(new Color(0xc0c0c0));
		setLayout(new BorderLayout());
		setOpaque(false);

		chessarray = new ChessArray();
		gridPanel = new GridPanel(chessarray);
		add(gridPanel);
		chessarray.addObserver(this);

		JPanel bottom = new JPanel();
		bottom.setOpaque(false);
		for (int i = 0; i < s1.length; i++) {
			button[i] = new JButton(s1[i]);
			button[i].addActionListener(this);
			bottom.add(button[i]);
		}
		add(bottom, BorderLayout.SOUTH);
		disablebutton();

		JPanel top = new JPanel();
		top.setOpaque(false);
		JLabel title = new JLabel("网络五子棋");
		title.setFont(new Font("华文琥珀", Font.TRUETYPE_FONT, 24));
		title.setForeground(new Color(0x123456));
		JLabel author = new JLabel("             华信智原");
		author.setFont(new Font("华文行楷", Font.TRUETYPE_FONT, 14));
		author.setForeground(Color.RED);
		top.add(title);
		top.add(author);
		add(top, BorderLayout.NORTH);

		String[] s2 = { "玩家1", "玩家2" };
		JPanel[] lr = new JPanel[2];
		for (int i = 0; i < s2.length; i++) {
			lr[i] = new JPanel();
			lr[i].setOpaque(false);
			lr[i].setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			JLabel image = new JLabel();
			image.setIcon(new ImageIcon(getClass().getResource("../image/photo" + i + ".png")));
			player[i] = new JLabel(s2[i]);
			gbc.gridx = 0;
			gbc.gridy = 0;
			gbc.insets = new Insets(10, 20, 10, 20);
			lr[i].add(image, gbc);
			gbc.gridy = 1;
			lr[i].add(player[i], gbc);
			gbc.gridy = 2;
			chessCase[i] = new JLabel();
			chessCase[i].setPreferredSize(new Dimension(black.getIconWidth(),black.getIconHeight()));
			lr[i].add(chessCase[i], gbc);
		}
		add(lr[0], BorderLayout.WEST);
		add(lr[1], BorderLayout.EAST);
	}

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Image img = new ImageIcon(getClass().getResource("../image/3.jpg")).getImage();
		g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof User) {
			User user = (User) arg;
			System.out.println(user.getName());
			if (o.equals(user)) {
				player[1].setText(user.getName());
			} else
				player[0].setText(user.getName());
			repaint();
		} else if (arg instanceof Integer) {
			int code = (int) arg;
			switch (code) {
			case StateCode.REQUEST:
				int f = JOptionPane.showConfirmDialog(this, "对方请求开始游戏，是否开始？", "游戏信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					chessarray.clearboard();
					SocketUtil.sendMessage(StateCode.AGREE);
					gridPanel.getPrompt().setMessage("");
					enablebutton();
					gridPanel.letgo = false;
					if (SocketUtil.getccolor().getRGB() == Color.black.getRGB())
						chessCase[0].setIcon(white);
					else
						chessCase[0].setIcon(black);
					chessCase[1].setIcon(null);
					o.addObserver(gridPanel);
					repaint();
				} else {
					SocketUtil.sendMessage(StateCode.REFUSE);
				}
				break;
			case StateCode.AGREE:
				chessarray.clearboard();
				enablebutton();
				gridPanel.getPrompt().setMessage("");
				o.addObserver(gridPanel);
				gridPanel.letgo = true;
				if (SocketUtil.getccolor().getRGB() == Color.black.getRGB())
					chessCase[1].setIcon(black);
				else
					chessCase[1].setIcon(white);
				chessCase[0].setIcon(null);
				repaint();
				break;
			case StateCode.REFUSE:
				JOptionPane.showMessageDialog(this, "你被拒绝了！");
				gridPanel.getPrompt().setMessage("请点击开始");
				break;
			case StateCode.WIN:
				gridPanel.getPrompt().setPrompt("你赢了", 82, Color.GREEN, 1f);
				gridPanel.letgo=false;
				disablebutton();
				repaint();
				break;
			case StateCode.LOSE:
				gridPanel.getPrompt().setPrompt("你输了", 82, Color.RED, 1f);
				gridPanel.letgo=false;
				disablebutton();
				repaint();
				break;
			case StateCode.DRAWNREQUEST:
				int s=JOptionPane.showConfirmDialog(this, "对方请求和棋，是否同意?");
				if(s==0){
					disablebutton();
					gridPanel.getPrompt().setPrompt("平局！", 90, Color.CYAN, 0.6f);
					gridPanel.letgo=false;
					SocketUtil.sendMessage(StateCode.DRAWNAGREE);
				}
				else{
					SocketUtil.sendMessage(StateCode.DRAWNREREFUSE);
				}
				break;
			case StateCode.DRAWNAGREE:
				gridPanel.getPrompt().setPrompt("平局!", 90, Color.CYAN, 0.6f);
				gridPanel.letgo=false;
				disablebutton();
				break;
			case StateCode.DRAWNREREFUSE:
				JOptionPane.showMessageDialog(this, "对方拒绝和棋！");
				gridPanel.getPrompt().setMessage("");
				break;
			case StateCode.HUIQIREQUEST:
				int x=JOptionPane.showConfirmDialog(this, "对方请求悔棋1颗，是否同意？");
				if(x==0){
					chessarray.remove();
					SocketUtil.sendMessage(StateCode.HUIQIAGREE);
					repaint();
				}
				else{
					SocketUtil.sendMessage(StateCode.HUIQIREFUSE);
				}
				break;
			case StateCode.HUIQIAGREE:
				gridPanel.getPrompt().setMessage("");
				chessarray.remove();
				repaint();
				break;
			case StateCode.HUIQIREFUSE:
				gridPanel.getPrompt().setMessage("");
				JOptionPane.showMessageDialog(this, "对方拒绝了你的悔棋请求！");
				repaint();
				break;
			case StateCode.REPLAYDONE:
				chessarray.clearboard();
				repaint();
				break;
			}
		} else if (arg instanceof String) {
			String s = (String) arg;
			if (s.equals("savesuccess")) {
				if (gridPanel.letgo) {
					if (SocketUtil.getccolor().getRGB() == Color.white.getRGB())
						chessCase[0].setIcon(black);
					else
						chessCase[0].setIcon(white);
					chessCase[1].setIcon(null);
				} else {
					if (SocketUtil.getccolor().getRGB() == Color.white.getRGB())
						chessCase[1].setIcon(white);
					else
						chessCase[1].setIcon(black);
					chessCase[0].setIcon(null);
				}

			}
			else if(s.equals("reset")){
				enablebutton();
				chessarray.clearboard();
				JOptionPane.showMessageDialog(this, "连接断开，等待重连！");
			}
		}
		else if(arg instanceof Color){
			Color cc=(Color) arg;
			if(cc.getRGB()==SocketUtil.getccolor().getRGB()){
				gridPanel.getPrompt().setPrompt("你赢了", 82, Color.GREEN, 1f);
				gridPanel.setEnabled(false);
				disablebutton();
			}
			else{
				gridPanel.getPrompt().setPrompt("你输了", 82, Color.RED, 1f);
				gridPanel.letgo=false;
				disablebutton();
			}
		}
		repaint();
	}

	private void disablebutton() {
		button[0].setEnabled(true);
		button[1].setEnabled(false);
		button[2].setEnabled(false);
		button[3].setEnabled(false);
		button[4].setEnabled(true);

	}

	private void enablebutton() {
		button[0].setEnabled(false);
		button[1].setEnabled(true);
		button[2].setEnabled(true);
		button[3].setEnabled(true);
		button[4].setEnabled(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getActionCommand();
		if (o.equals("开始")) {
			ChessArray.chesslist.clear();
			chessarray.clearboard();
			SocketUtil.sendMessage(StateCode.REQUEST);
			gridPanel.getPrompt().setPrompt("等待对方同意", 48, Color.BLACK, 0.9f);
			repaint();
		} else if (o.equals("悔棋")) {
			SocketUtil.sendMessage(StateCode.HUIQIREQUEST);
			gridPanel.getPrompt().setPrompt("等待对方处理悔棋请求", 48, Color.ORANGE, 0.6f);
			repaint();
		} else if (o.equals("和棋")) {
			SocketUtil.sendMessage(StateCode.DRAWNREQUEST);
			gridPanel.getPrompt().setPrompt("等待对方处理和棋请求", 60, Color.BLACK, 0.9f);
			repaint();
		} else if (o.equals("认输")) {
			SocketUtil.sendMessage(StateCode.WIN);
			gridPanel.getPrompt().setMessage("你输了！");
			gridPanel.letgo=false;
			disablebutton();
			repaint();
		} else if (o.equals("回放")) {
			gridPanel.getPrompt().setMessage("");
			gridPanel.letgo=false;
			chessarray.replay();
		}
	}
}
